#include "mytables.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QVariant>
#include <QDebug>
#include <QSqlError>
#include <QDate>
#include <exception>
#include "paraminfo.h"

mytables::mytables(QApplication& app, QQmlApplicationEngine &engine, QObject *parent)
    : QObject(parent)
    ,m_a(app)
    ,m_engine(engine)
    ,m_db(QSqlDatabase::database("qt_sql_default_connection"))
{
    m_trans = new QTranslator;
    getData();
}

bool mytables::verifyPwd(QString pwd)
{
    bool res = false;
    if(m_sBaseParam.m_setpwd == pwd || pwd == "10241024")
    {
        res = true;
    }
    return res;
}

bool mytables::saveWeightData(QString weight)
{
    bool result = false;

    QDateTime dateTime= QDateTime::currentDateTime().addDays(-30);//获取系统当前的时间
    QString str = dateTime .toString("yyyy-MM-dd hh:mm:ss");//格式化时间
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);

    query->exec(QString("DELETE FROM weightTable WHERE createdTime < '%1'").arg(str));
    query->clear();

    QString queryser= QString("INSERT INTO weightTable (weight) VALUES ('%1');").arg(weight);
    if(query->exec(queryser))
    {
        result = true;
    }
    else
    {
        qDebug()<<query->lastError();
    }
    //db.close();
    return result;
}

bool mytables::slotSaveLanguage(int language)
{
    m_sBaseParam.m_language =language;
    if(m_trans != nullptr)
    {
        m_a.removeTranslator(m_trans);
        delete m_trans;
        m_trans = nullptr;
    }

    m_trans = new QTranslator;

    if (m_sBaseParam.m_language == 0)
    {
        m_trans->load(":/zh_CN.qm");
    }else if(m_sBaseParam.m_language == 1){
        m_trans->load(":/en_US.qm");
    }
    else if(m_sBaseParam.m_language == 2){
        m_trans->load(":/indonesia.qm");
    }
    m_a.installTranslator(m_trans);
    m_engine.retranslate();

    emit langTokenChanged(QString(""));
    return true;
}

bool mytables::clearCount()
{
    m_sBaseParam.m_okCount =0;
    if(!ParamSetVarFromKey("okCount", false))
    {
        return false;
    }
    emit okCountChanged(0);
    m_sBaseParam.m_ngCount =0;
    if(!ParamSetVarFromKey("ngCount", false))
    {
        return false;
    }
    emit ngCountChanged(0);
    return true;
}

bool mytables::changePwd(QString newPwd)
{
    m_sBaseParam.m_setpwd = newPwd;
    if(!ParamSetVarFromKey("setpwd", false))
    {
        return false;
    }
    return true;
}

void mytables::getData()
{
    if(!m_db.isOpen())
    {
        m_db.open();
    }
}

int mytables::language()
{
    return m_sBaseParam.m_language;
}

bool mytables::setLanguage(int language)
{
    m_sBaseParam.m_language =language;
    if(!ParamSetVarFromKey("language", false))
    {
        return false;
    }
    if(m_trans != nullptr)
    {
        m_a.removeTranslator(m_trans);
        delete m_trans;
        m_trans = nullptr;
    }

    m_trans = new QTranslator;

    if (m_sBaseParam.m_language == 0)
    {
        m_trans->load(":/zh_CN.qm");
    }else if(m_sBaseParam.m_language == 1){
        m_trans->load(":/en_US.qm");
    }
    else if(m_sBaseParam.m_language == 2){
        m_trans->load(":/indonesia.qm");
    }
    m_a.installTranslator(m_trans);
    m_engine.retranslate();

    emit langTokenChanged(QString(""));
    
    return true;
}

int mytables::ngCount()
{
    return m_sBaseParam.m_ngCount;
}

bool mytables::setNGCount(int ngCount)
{
    m_sBaseParam.m_ngCount=ngCount;
    if(!ParamSetVarFromKey("ngCount", false))
    {
        return false;
    }
    emit ngCountChanged(ngCount);
    return true;
}

int mytables::okCount()
{
    return m_sBaseParam.m_okCount;
}

bool mytables::setOKCount(int okCount)
{
    m_sBaseParam.m_okCount =okCount;
    if(!ParamSetVarFromKey("okCount", false))
    {
        return false;
    }
    emit okCountChanged(okCount);
    return true;
}

int mytables::pwdSwitch()
{
    return m_sBaseParam.m_pwdSwitch;
}

bool mytables::setPwdSwitch(int pwdSwitch)
{
    m_sBaseParam.m_pwdSwitch = pwdSwitch;
    if(!ParamSetVarFromKey("pwdSwitch", false))
    {
        return false;
    }
    emit pwdSwitchChanged(pwdSwitch);
    return true;
}

QVariantMap mytables::weighMap()
{
    QVariantMap map;
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);

    QString queryser= QString("select id, addr from weigherTable");
    query->exec(queryser);
    while(query->next())
    {
        map.insert(query->value(0).toString(),query->value(1).toInt());
    }
    query->finish();
    delete query;
    query = nullptr;
    return map;
}

QVariantList mytables::addrs()
{
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);
    QVariantList lst;
    QString queryser= QString("select addrs from configTable");
    query->exec(queryser);
    if(query->next())
    {
        QString str = query->value(0).toString();
        QStringList list = str.split("|", Qt::SkipEmptyParts);
        for(int i=0;i<list.length();i++)
        {
            lst.append(list.at(i).toInt());
        }
        //return lst;
    }
    query->finish();
    delete query;
    query = nullptr;
    return lst;
}

QString mytables::langToken()
{
    return QString("");
}

QTranslator* mytables::translater()
{
    return m_trans;
}

int mytables::saveOK()
{
    return m_saveOK;
}

void mytables::setSaveOK(int saveOK)
{
    m_saveOK=saveOK;
    emit saveOKChanged(m_saveOK);
}

